package cn.darkhorse.core.param;

import cn.darkhorse.core.consts.BpmnBusinessConsts;
import cn.darkhorse.entity.BpmnWorkflowParam;
import cn.darkhorse.exception.InteractionFailedException;
import cn.darkhorse.workflow.auto.entity.BpmnDeptRelative;
import cn.darkhorse.workflow.auto.entity.BpmnSysUser;
import cn.darkhorse.workflow.auto.service.IBpmnDeptRelativeService;
import cn.darkhorse.workflow.auto.service.IBpmnSysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuanjun
 * @since 2020/6/3 17:22
 */

public class ParserStrategy4TaskGroup implements IParamParserStrategy {
    protected IUser iUser;

    public ParserStrategy4TaskGroup(IUser iUser) {
        this.iUser = iUser;
    }

    @Override
    public Map<String, Object> parse(BpmnWorkflowParam param) {
        /*Map<String, Object> resultMap = new HashMap<>(2);

        String paramName = param.getParamName();
        List<String> users = iUser.getUsers(param.getParamValue());
        if (users != null && users.size() > 0) {
            resultMap.put(paramName, users);
            return resultMap;
        }
        return null;*/
        return getMaps(param,null);
    }

    @Override
    public <T> Map<String, Object> parse(BpmnWorkflowParam param, Class<T> tClazz, T form,String userName) {
       // return this.parse(param);
        return getMaps(param,userName);
    }
    
    private Map<String, Object> getMaps(BpmnWorkflowParam param,String userName){
        /*if(StringUtils.isBlank(userName)){
            throw new InteractionFailedException("相对岗位对应的用户不存在！");
        }*/
        Map<String, Object> resultMap = new HashMap<>(2);
        String paramName = param.getParamName();
        List<String> users = iUser.getUsers(userName,param.getParamValue());
        if (users != null && users.size() > 0) {
            resultMap.put(paramName, users);
            //部门经理和领导只有一个
            if(BpmnBusinessConsts.RELATIVE_POST_LEADER.equals(paramName) || BpmnBusinessConsts.RELATIVE_POST_DEPARTMENT_MANAGER.equals(paramName)){
                 resultMap.put(paramName, users.get(0));
            }
           return resultMap;
        }else{
            users.add("空");
            if(BpmnBusinessConsts.RELATIVE_POST_LEADER.equals(paramName) || BpmnBusinessConsts.RELATIVE_POST_DEPARTMENT_MANAGER.equals(paramName)){
                resultMap.put(paramName, users.get(0));
            }else{
                resultMap.put(paramName, users);
            }
            return resultMap;
        }
        //return null;
    }
 
    
    
  /*  private <T> Long  getDeptId( Class<T> tClazz, T form){
        String fieldName = "operatorDept";
        Field[] fields = tClazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                if (fieldName.equals(field.getName())) {
                    Long value = (Long)field.get(form);
                    return value;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                // todo 需要抛异常
            }
        }
        return null;
    }*/
    
    
    
    
    
}
